package com.springcloud.zuul.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.springcloud.zuul.common.constant.ServiceCode;
import com.springcloud.zuul.entity.SysRoleMenuEntity;
import com.springcloud.zuul.exception.ServiceException;
import com.springcloud.zuul.mapper.SysRoleMenuDao;
import com.springcloud.zuul.service.SysRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 角色与菜单对应关系
 *
 */
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuDao, SysRoleMenuEntity> implements SysRoleMenuService {

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveOrUpdate(String roleId, List<String> menuIdList) {
		//先删除角色与菜单关系
		deleteBatch(Lists.newArrayList(roleId));

		//产品权限功能不能为空，无意义数据
		if(menuIdList.size() == 0){
			throw new ServiceException(ServiceCode.PRODUCT_AUTHORITY);
		}

		//保存角色与菜单关系
		for(String menuId : menuIdList){
			SysRoleMenuEntity sysRoleMenuEntity = new SysRoleMenuEntity();
			sysRoleMenuEntity.setMenuId(menuId);
			sysRoleMenuEntity.setRoleId(roleId);

			this.save(sysRoleMenuEntity);
		}
	}

	/**
	 * Description:根据角色ID，获取菜单ID列表
	 * @author zhlu
	 * @date 2020/1/7 5:11 下午
	 * @param roleList
	 * @return java.util.List<java.lang.String>
	 */
	@Override
	public List<String> queryMenuIdList(List<String> roleList) {
		return baseMapper.queryMenuIdList(roleList);
	}

	@Override
	public int deleteBatch(List<String> roleIds){
		return baseMapper.deleteBatch(roleIds);
	}

}
